package com.epoint.mobileoa.ui.attachmanager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.format.DateFormat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.adapter.AttachManagerListAdp;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.IOHelp;

/**
 * 
 * @author lilin
 * @date 2012-5-11 下午12:23:37
 * @annotation 已下载附件列表
 */
public class AttachListView extends SuperView {

	private ListView lv;
	private String ATTACH_PATH;
	public static List<Map<String, Object>> mData;

	@Override
	public void initControls(){
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setText("确定");
		top_left_bt.setOnClickListener(this);
		top_right_bt.setOnClickListener(this);

		ATTACH_PATH = "/sdcard/" + getString(R.string.sdforder)
				+ "/attachdownload";
		lv = (ListView) findViewById(R.id.lv_mailstyle);
		IOHelp.FoldCreate(ATTACH_PATH);
		lv.setOnItemLongClickListener(this);
		lv.setOnItemClickListener(this);
		refreshData();
	}

	@Override
	public void onClick(View v) {

		if (v == top_right_bt) {
			finish();

		} else if (v == top_left_bt) {
			mData.clear();
			AttachManagerListAdp.isSelected = null;
			finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		String filepath = mData.get(position).get("path").toString();
//		IOHelp.doOpenFile(AttachListView.this, filepath);
	}

	public void refreshData() {
		mData = getData(ATTACH_PATH);
		AttachManagerListAdp adapter = new AttachManagerListAdp(this, mData);
		lv.setAdapter(adapter);
	}

	private List<Map<String, Object>> getData(String dir) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();

		File file = new File(dir);

		File[] files = file.listFiles();// 所有文件夹

		for (File currentFile : files) {
			if (currentFile.isDirectory()) {
				File[] subfiles = currentFile.listFiles();
				for (File subfile : subfiles) {
					if (!subfile.isDirectory()) {
						map = new HashMap<String, Object>();
						map.put("path", subfile.getAbsolutePath());
						map.put("title", subfile.getName());
						Long time = subfile.lastModified();
						// 格式化时间字符串
						String timestr = DateFormat.format(
								"yyyy.MM.dd kk:mm:ss", time).toString();
						map.put("info", timestr);
						map.put("icon", R.drawable.img_file_48);
						String hz = subfile.getName();
						if (hz.lastIndexOf(".doc") > -1
								|| hz.lastIndexOf(".docx") > -1) {
							map.put("icon", R.drawable.img_attach_doc);
						} else if (hz.lastIndexOf(".xls") > -1
								|| hz.lastIndexOf(".xlsx") > -1) {
							map.put("icon", R.drawable.img_attach_excel);
						} else if (hz.lastIndexOf(".ppt") > -1) {
							map.put("icon", R.drawable.img_attach_ppt);
						} else if (hz.lastIndexOf(".rar") > -1) {
							map.put("icon", R.drawable.img_rar_48);
						} else if (hz.lastIndexOf(".zip") > -1) {
							map.put("icon", R.drawable.img_zip_48);
						} else if (hz.lastIndexOf(".jpeg") > -1
								|| hz.lastIndexOf(".jpg") > -1) {
							map.put("icon", R.drawable.img_jpg_48);
						} else if (hz.lastIndexOf(".png") > -1) {
							map.put("icon", R.drawable.img_pngb_48);
						} else if (hz.lastIndexOf(".mp3") > -1) {
							map.put("icon", R.drawable.img_mp3b_48);
						} else if (hz.lastIndexOf(".txt") > -1) {
							map.put("icon", R.drawable.img_txt_48);
						} else if (hz.lastIndexOf(".pdf") > -1) {
							map.put("icon", R.drawable.img_pdf_48);
						} else if (hz.lastIndexOf(".html") > -1) {
							map.put("icon", R.drawable.img_html_48);
						}

						list.add(map);
					}
				}
			}
		}
		return list;
	}

	@Override
	public void refresh(Object... param) {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.layout_mailstyle;
	}

	@Override
	public String setTitle() {
		return "已下载附件";
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
